<template>
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a @click="changeBrand(item.id)" :class="{ active: item.id === filterData.selectedBrand }" href="javascript:;"
          v-for="item in filterData.brands" :key="item.id">{{ item.name }}</a>
      </div>
    </div>
    <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
      <div class="head">{{ item.name }}：</div>
      <div class="body">
        <a @click="changeProp(item, prop.id)" :class="{ active: prop.id === item.selectedProp }" href="javascript:;"
          v-for="prop in item.properties" :key="prop.id">{{ prop.name }}</a>
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <skeleton class="item" width="800px" height="40px" />
    <skeleton class="item" width="800px" height="40px" />
    <skeleton class="item" width="600px" height="40px" />
    <skeleton class="item" width="600px" height="40px" />
    <skeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script setup >
import { ref, watchEffect } from 'vue';
import { useRoute } from 'vue-router';
import Category from '@/api/modules/category';

const route = useRoute()
const filterData = ref(null)
const filterLoading = ref(false)
const emit = defineEmits(["filterChange"])
const getFilterData = (currentSubId) => {
  Category.subCategoryList(currentSubId).then(({ code, result }) => {
    if (code == 1) {
      result.selectedBrand = null
      result.brands.unshift({ id: null, name: '全部' })
      result.saleProperties.forEach(item => {
        item.selectedProp = null
        item.properties.unshift({ id: null, name: '全部' })
      })
      filterData.value = result
      filterLoading.value = false
    }
  })
}

watchEffect(() => {
  const currentSubId = route.params.id
  if (currentSubId && `/category/sub/${currentSubId}` === route.path) {
    getFilterData(currentSubId)
    filterLoading.value = true
  }

})

const changeProp = (item, propId) => {
  if (item.selectedProp === propId) return
  item.selectedProp = propId
  emit('filterChange', getFilterParams())
}
const changeBrand = (brandId) => {
  if (filterData.value.selectedBrand === brandId) return
  filterData.value.selectedBrand = brandId
  emit('filterChange', getFilterParams())
}


// 获取筛选参数的函数
const getFilterParams = () => {
  const obj = { brandId: null, attrs: [] }
  // 品牌
  obj.brandId = filterData.value.selectedBrand
  // 销售属性
  filterData.value.saleProperties.forEach(item => {
    if (item.selectedProp) {
      const prop = item.properties.find(prop => prop.id === item.selectedProp)
      obj.attrs.push({ groupName: item.name, propertyName: prop.name })
    }
  })
  // 参考数据：{brandId:'',attrs:[{groupName:'',propertyName:''},...]}
  if (obj.attrs.length === 0) obj.attrs = null
  return obj
}
</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;

  .item {
    display: flex;
    line-height: 40px;

    .head {
      width: 80px;
      color: #999;
    }

    .body {
      flex: 1;

      a {
        margin-right: 36px;
        transition: all .3s;
        display: inline-block;

        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }

  .xtx-skeleton {
    padding: 10px 0;
  }
}
</style>
